package com.wuliupai.delivery.wuliupaidelivery.db;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.R;

import java.util.List;

import ca.barrenechea.widget.recyclerview.decoration.StickyHeaderAdapter;


/**
 * Descriptions：地址选择界面中城市列表的Adapter
 * <p>
 * Author：ChenME
 * Date：12/09/2016
 * Email：ibelieve1210@163.com
 */
public class CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder> implements
        StickyHeaderAdapter<CityAdapter.HeaderHolder> {

    private LayoutInflater mInflater;
    private List<City> namelist;

    private char lastChar = '\u0000';
    private int DisplayIndex = 0;

    private OnItemClickListener mOnItemClickListener;

    public CityAdapter(Context context, List<City> namelist) {
        mInflater = LayoutInflater.from(context);
        this.namelist = namelist;
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        final View view = mInflater.inflate(R.layout.item_select_place_recyclerview_item, viewGroup, false);

        return new ViewHolder(view);
    }

    //条目文本填充
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.itemClick(viewHolder.item, i);
            }
        });

        viewHolder.item.setText(namelist.get(i).getCityName());
    }

    @Override
    public int getItemCount() {
        return namelist.size();
    }

    public long getHeaderId(int position) {

        //这里面的是如果当前position与之前position重复（内部判断）  则不显示悬浮标题栏  如果不一样则显示标题栏

        char ch = namelist.get(position).firstLetter.charAt(0);

        if (lastChar == '\u0000') {

            lastChar = ch;

            return DisplayIndex;
        } else {

            if (lastChar == ch) {

                return DisplayIndex;

            } else {

                lastChar = ch;
                DisplayIndex++;
                return DisplayIndex;
            }

        }

    }

    public HeaderHolder onCreateHeaderViewHolder(ViewGroup parent) {
        final View view = mInflater.inflate(R.layout.item_select_place_recyclerview_header, parent, false);
        return new HeaderHolder(view);
    }

    //悬浮标题栏填充文本
    public void onBindHeaderViewHolder(HeaderHolder viewholder, int position) {
        viewholder.header.setText(namelist.get(position).firstLetter.charAt(0) + "");
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView item;

        public ViewHolder(View itemView) {
            super(itemView);

            item = (TextView) itemView;
        }
    }

    static class HeaderHolder extends RecyclerView.ViewHolder {
        public TextView header;

        public HeaderHolder(View itemView) {
            super(itemView);

            header = (TextView) itemView;
        }
    }

    /**
     * 获得指定首字母的位置
     *
     * @param ch
     * @return
     */
    public int getPositionForSection(char ch) {

        for (int i = 0; i < getItemCount(); i++) {
            char firstChar = namelist.get(i).firstLetter.charAt(0);
            if (firstChar == ch) {
                return i;
            }
        }
        return -1;

    }

    public interface OnItemClickListener {
        void itemClick(View view, int pos);
    }

}
